package com.sptpc.demo1.controller;

import com.sptpc.demo1.controller.requests.NewOrderRequest;
import com.sptpc.demo1.model.Order;
import com.sptpc.demo1.service.IDrinkService;
import com.sptpc.demo1.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    public IOrderService orderService;
    @Autowired
    public IDrinkService drinkService;

    //查询
    @GetMapping("/")
    public List<Order> getAllOrder() {
        return orderService.getALLOrders();
    }

    //下单————添加订单
    @PostMapping("/userorder")
    public Order userOrders(@RequestBody NewOrderRequest request) {
        log.info("订单信息{}", request);
        Order saveorder = new Order();//保存的的Orders
        BeanUtils.copyProperties(request, saveorder);
        orderService.addNewOrder(saveorder, request.getItems());
        return saveorder;
    }

    /**
     * 通过用户名查询饮品订单
     * get /order/张三
     * @param username 用户名
     * @return 查询到的订单
     */
    @GetMapping("/{username}")
    public List<Order> findOrderByUserName(@PathVariable String username) {
        log.info("用户名{}", username);
        return orderService.findByUserName(username);
    }

    /**
     * TODO:用户传入对象  Order对象内有的参数
     * 多条件查询替代 OrderRepository中穷举完成查询方法
     * 通过对象查询订单有相同设置条件在设置查询
     */
    @PostMapping("/selectorders")
    public List<Order> findOrder(@RequestBody Order selectOrderRequest) {
        log.info("查询对象{}", selectOrderRequest);
        return orderService.findOrder(selectOrderRequest);
    }

    /**
     * @param orderID 订单号
     * @param stateID 修改订单状态值
     * @return 修改完成后的订单信息
     */
    @PutMapping("/setstate{orderID}/{stateID}")
    public Order setDrinkOrderState(@PathVariable Integer orderID, @PathVariable Integer stateID) {
        // TODO:将订单号传入实体类实现查询
        Order order = new Order();
        order.setId(orderID);
        log.info("修改订单状态的ID{}", order);
        return orderService.setOrderState(orderService.findOrder(order).get(0), stateID);
    }
    //删除订单
    @DeleteMapping("/{id}")
    public String delOrder(@PathVariable Integer id) {
        log.info("删除的订单号{}", id);
        orderService.delOrderByID(id);
        return "删除成功";
    }
}
